package com.hcq.service;

import com.alibaba.fastjson.JSON;
import com.common.base.BaseService;
import com.common.entity.PageParam;
import com.common.entity.Rsp;
import com.common.entity.Scenic;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hcq.mapper.ScenicMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Service
public class ScenicServiceImpl implements BaseService<Scenic> {

    @Autowired
    ScenicMapper scenicMapper;

    @Autowired
    private RedisServiceImpl redisService;

    @Override
    public PageInfo<Scenic> searchSomeByPage(PageParam<Scenic> pageParam) {
        if(!StringUtils.isEmpty(pageParam.getSuperSearchKeyWord())){
            //执行高级检索
            //分页
            PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
            //排序
            for(int i=0;i<pageParam.getOrderParams().length;i++){
                PageHelper.orderBy(pageParam.getOrderParams()[i]) ;
            }


            List<Scenic> scenicList=scenicMapper.superSearch(pageParam.getSuperSearchKeyWord());
            PageInfo<Scenic> scenicPageInfo = new PageInfo<>(scenicList);

            return scenicPageInfo;

        }else{
            //分页
            PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
            //排序
            for(int i=0;i<pageParam.getOrderParams().length;i++){
                PageHelper.orderBy(pageParam.getOrderParams()[i]);
            }

            Scenic scenic = new Scenic();
            scenic.setDeleted(0);
            pageParam.setModel(scenic);
            List<Scenic> scenicList=scenicMapper.searchSomeByPage(pageParam.getModel());
            PageInfo<Scenic> scenicPageInfo = new PageInfo<>(scenicList);

            return scenicPageInfo;
        }
    }

    @Override
    public Scenic getOneById(String id) {
        return scenicMapper.getOneById(id);
    }

    @Override
    public Rsp addOne(Scenic scenic) {
        scenic.setId(UUID.randomUUID().toString().replaceAll("-",""));
        scenic.setStatus(0);
        scenic.setDeleted(0);
        int result = scenicMapper.addOne(scenic);
        return result == 1?Rsp.success("添加成功"):Rsp.fail().msg("添加失败");
    }

    @Override
    public Rsp update(Scenic scenic) {
        int result = scenicMapper.update(scenic);
        return result == 1?Rsp.success("修改成功"):Rsp.fail().msg("修改失败");
    }

    @Override
    public Rsp deleteLogical(Scenic scenic) {
        scenic.setDeleted(1);
        int result = scenicMapper.update(scenic);
        return result == 1?Rsp.success("删除成功"):Rsp.fail().msg("删除失败");
    }

    public String test(List<Scenic> scenics) {
        for(Scenic s : scenics){
            s.setId(UUID.randomUUID().toString().replaceAll("-",""));
            s.setStatus(0);
            s.setContactName("陈豪");
            scenicMapper.addOne(s);
        }
        return "111";
    }

    public Rsp findHotScenic(){

        boolean exists = redisService.exists("hot");

        if (exists){
            System.out.println("删除");
            redisService.remove("hot");
        }

        //redisService.lRange()


        List<Scenic> all = scenicMapper.findAll();
        List<String> str = new ArrayList<>(10);

        for (Scenic scenic : all) {
            String s = JSON.toJSONString(scenic);
            str.add(s);
        }
        redisService.lPush("hot",str);
        return all != null?Rsp.success(all):Rsp.fail().msg("查询失败");
    }
}
